package com.asa.fushu.bean;


import java.util.Scanner;
 
public class Fushu {
	public double real;
	public double im;
 
	public Fushu() {
	}
 
	public Fushu(double real, double im) {
		this.real = real;
		this.im = im;
	}
 
	public Fushu add(Fushu c) {
		Fushu com = new Fushu(0, 0);
		com.real = c.real + this.real;
		com.im = c.im + this.im;
		return com;
 
	} 
 
	public Fushu add(double real) {
		Fushu com = new Fushu(0, 0);
		com.real = this.real + real;
		com.im = this.im;
		return com;
	}
	public Fushu add(Fushu com1, Fushu com2) {
		Fushu com = new Fushu(0, 0);
		com.real = com1.real + com2.real;
		com.im = com1.im + com2.im;
		return com;
	}
 
	public Fushu sub(double real) {
		Fushu com = new Fushu(0, 0);
		com.real = this.real - real;
		com.im = this.im;
		return com;
	}
	public Fushu sub(Fushu com2) {
		Fushu com = new Fushu(0, 0);
		com.real = this.real - com2.real;
		com.im = this.im - com2.im;
		return com;
 
	}
	
	/**
	 * 复数的减法
	 * @param com1
	 * @param com2
	 * @return
	 */
	public Fushu sub(Fushu com1, Fushu com2) {
		Fushu com = new Fushu(0, 0);
		com.real = com1.real - com2.real;
		com.im = com1.im - com2.im;
		return com;
	}
 
	/**
	 * 复数的乘法
	 * @param com1
	 * @param com2
	 * @return
	 */
	public Fushu mul(Fushu com1, Fushu com2) {
		Fushu com = new Fushu(0, 0);
		com.real = com1.real *
				com2.real;
		com.im = com1.im * com2.im;
		com.real = com.real - com.im;
		com.im = com1.real*com2.im + com1.im*com2.real ;
		return com;
 
	}
	public Fushu mul(Fushu com2) {
		Fushu com = new Fushu(0, 0);
		com.real = this.real *
				com2.real;
		com.im = this.im * com2.im;
		com.real = com.real - com.im;
		com.im = this.real*com2.im + this.im*com2.real ;
		return com;
 
	}
	
	
	/**
	 * 复数的除法
	 * @param com1
	 * @param com2
	 * @return
	 */
	public Fushu divide(Fushu com1, Fushu com2) {
		
		double chushu = com2.real*com2.real + com2.im*com2.im;
		Fushu com22 = new Fushu(0, 0);
		com22.real = com2.real;
		com22.im = -com2.im;
		Fushu com = mul(com1, com22);
		com.real = com.real/chushu;
		com.im = com.im/chushu;
		return com;
 
	}
	
	public Fushu divide(Fushu com2) {
		
		double chushu = com2.real*com2.real + com2.im*com2.im;
		Fushu com22 = new Fushu(0, 0);
		com22.real = com2.real;
		com22.im = -com2.im;
		Fushu com = mul(com22);
		com.real = com.real/chushu;
		com.im = com.im/chushu;
		return com;
 
	}
	
	
	
	
	

	public double length() {
		double im2 = this.getIm();
		double real2 = this.getReal();
		
		return Math.pow(im2*im2+real2*real2, 0.5);
	}
	
	
	/**
	 * 共轭
	 * @param com1
	 * @return
	 */
	public Fushu conjugate(Fushu com1) {
		
		Fushu com22 = new Fushu(0, 0);
		com22.real = com1.real;
		com22.im = -com1.im;
		return com22;
	}
	
	
	/**
	 * 打印当前复数
	 */
	public void shownumber() {
		if (this.im > 0)
			System.out.print(this.real + "+" + this.im + "i");
		else if (this.im < 0)
			System.out.print(this.real + "" + this.im + "i");
		else if (this.im == 0&&this.real != 0)
			System.out.print(this.real);
		else if (this.real == 0&&this.im != 0)
			System.out.print(this.im + "i");
		else {
			System.out.print(0);

		}
		System.out.println();
	}
	
	
	
	
	
 
	public Fushu innumber(String str) {
		String str1;
		boolean flag1;
		int flag2 = 0;
		int flag3 = 0 ;
		boolean flag4 = true;
		double real = 0, im = 0;
		double num[] = new double[2];
		String b[] = new String[2];
		Fushu com = new Fushu(0, 0);
		char a[] = str.toCharArray();
		for (int i = 0; i < a.length; i++) {
			if ('i' == a[0]) {
				real = 0;
				im = 1;
				flag3=11;
				break;
			}
			else if(a[0]=='-'){
				real = 0;
				im = -1;
				flag3=11;
				break;
				
			}
			else if (((a[a.length - 2]) < '0') || ((a[a.length - 2]) > '9')) {
				if (a[a.length - 2] == '-') {
					str1 = str.substring(0, a.length - 2);
					b = str1.split("\\-");
					real = Double.parseDouble(b[0]);
					im = -1;
					flag3=11;
					break;
				} 
				
				else if (a[a.length - 2] == '+') {
					str1 = str.substring(0, a.length - 1);
					b = str1.split("\\+");
					real = Double.parseDouble(b[0]);
					im = 1;
					flag3=11;
					break;
				}
				} else if ((a[i] >= '0' && a[i] <= '9') || a[i] == '+' || a[i] == '-' || a[i] == '.' || a[i] == 'i') {
					if (a[i] == '+') {
						str1 = str.substring(0, a.length - 1);
						b = str1.split("\\+");
						flag2 = 1;
					} else if (a[i] == '-') {
						flag2 = -1;
						str1 = str.substring(0, a.length - 1);
						if (i == 0) {
							flag4 = false;
							b[1] = str1;
						} else {
							b = str1.split("\\-");
							flag4 = true;
						}
					} else if (a[i] == 'i') {
						flag3++;
 
					}
				} 
				else {
					System.out.println("输入错误11111");
					flag1 = false;
					break;
				}
			}
		switch (flag3) {
		case 0:
			b[0] = str;
			real = Double.parseDouble(b[0]);
			break;
		case 1:
			switch (flag2) {
			case -1:
				if (flag4) {
					for (int k = 0; k < 2; k++) {
						num[k] = Double.parseDouble(b[k]);
					}
					real = num[0];
					im = -num[1];
				} else {
					real = 0;
					im = Double.parseDouble(b[1]);
				}
 
				break;
			case 1:
				for (int k = 0; k < 2; k++) {
					num[k] = Double.parseDouble(b[k]);
				}
				real = num[0];
				im = num[1];
				break;
			case 0:
				b[0] = str.substring(0, a.length - 1);
				im = Double.parseDouble(b[0]);
				real = 0;
				break;
			default:
				System.out.println("特殊异常，请处理！！！！！！！！！！！！！！！！！！");
				break;
			}
			break;
 
		case 11: break;
		default:
			System.out.println("输入错误22222");
			flag1 = false;
			break;
		}
    com.real=real;
    com.im=im;
    return com;
		
	}
	
	
	public static void shownumber(Fushu c) {
		if (c.im > 0)
			System.out.println(c.real + "+" + c.im + "i");
		else if (c.im < 0)
			System.out.println(c.real + "" + c.im + "i");
		else if (c.im == 0)
			System.out.println(c.real);
		if (c.real == 0)
			System.out.println(c.im + "i");
 
	}
	
	
	/**
	 * 计算模长
	 * @param com1
	 * @return
	 */
	public static double length(Fushu com1) {
		double im2 = com1.getIm();
		double real2 = com1.getReal();
		
		return Math.pow(im2*im2+real2*real2, 0.5);
	}
	
	
	
	
	
	
	
public static void main(String[]args){
	//public void window() {
		boolean flag=true;
		Fushu w1=new Fushu();
		Fushu w2=new Fushu();
		while(flag){
		System.out.println("---------------欢迎使用虚数算法-------------");
		System.out.println("----------------选择所选功能---------------");
		System.out.println("-------1.加法运算------------2.减法运算-----");
		System.out.println("-------3.乘法运算------------4.退出程序-----");
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		switch (a) {
		case 1:
			System.out.println("输入需要相加的两数：");
			System.out.println("请输入第一个数：");
			String str=sc.next();
			w1=w1.innumber(str);
			System.out.println("请输入第二个数：");
			String str1=sc.next();
			w2=w2.innumber(str1);
			System.out.println("结果为：");
			w1.shownumber(w1.add(w1,w2));
			break;
		case 2:
			System.out.println("输入需要相减的两数：");
			System.out.println("请输入第一个数：");
			String str21=sc.next();
			w1=w1.innumber(str21);
			System.out.println("请输入第二个数：");
			String str22=sc.next();
			w2=w2.innumber(str22);
			System.out.println("结果为：");
			w1.shownumber(w1.sub(w1,w2));
			break;
		case 3:
			System.out.println("输入需要相乘的两数：");
			System.out.println("请输入第一个数(x+/-yi)：");
			String str31=sc.next();
			w1=w1.innumber(str31);
			System.out.println("请输入第二个数(x+/-yi)：");
			String str32=sc.next();
			w2=w2.innumber(str32);
			System.out.println("结果为：");
			w1.shownumber(w1.mul(w1, w2));
			break;
 
		 case 4: System.out.println("已退出程序！");
		         flag=false;
			break;
		default: System.out.println("选择无效，请重新选择！");
			break;
		}
		}
	}

	public double getReal() {
		return real;
	}
	
	public double getIm() {
		return im;
	}


 


	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		
		if (obj instanceof Fushu) {
			Fushu f = (Fushu) obj;
						
			
			return this.real==f.real && this.im==f.im;
			
		}
		
		return super.equals(obj);
	}

	@Override
	public String toString() {
		return "Fushu [real=" + real + ", im=" + im + "]";
	}






}
